import os
import random
import math
import pygame
from pygame.locals import *

import Globals
import LightSnowBall

INDOCK = 0
ATCREATION = 1
ALIVE = 2
RUNNING = 3

class Tux (pygame.sprite.Sprite):
	"""Just a tux"""
	def __init__(self): #, state=ATCREATION
		pygame.sprite.Sprite.__init__(self) # Initialize base class
		#Globals.ObjectsExists.add(self)
		tuxFile = os.path.join('data', "tux.png")
		self.tuxImage = pygame.image.load(tuxFile).convert_alpha()
		#self.state = state
		#if self.state == ATCREATION:
		self.generateCreationImage()
		self.react = 0
		self.reloadCount = 0

	def getImage(self):
		if self.state == INDOCK:
			pass
		elif self.state == ATCREATION:
			return self.creationImage
		elif self.state == ALIVE:
			return self.tuxImage
		elif self.state == RUNNING:
			return self.tuxImage

	def getState(self):
		return self.state

	def create(self, position):
		"""Makes this object alive"""
		#Globals.ObjectsSelectable.add(self)
		Globals.ObjectsArctic.add(self)
		self.rect = self.tuxImage.get_rect()
		self.state = ALIVE
		self.rect.center = position
		Globals.NumberOfTuxs -= 1

	#_state = INDOCK
	image = property(getImage, doc="Returns the image used to draw this sprite")
	#state = property(getState, doc="State of this instance")

	shootingRange = 100

	def update(self):
		if self.state == INDOCK:
			pass
		elif self.state == ATCREATION:
			self.rect.center = pygame.mouse.get_pos()
		elif self.state == ALIVE:
			self.updateAlive()
		elif self.state == RUNNING:
			self.updateRunning()

	def updateAlive(self):
		if pygame.sprite.spritecollideany(self, Globals.ObjectsTropical):
			self.state = RUNNING
		if self.reloadCount == 0: #self.react > 5 and self.reloadCount == 0:
			#self.react = 0
			self.rangeCollision()
		elif self.reloadCount != 0:
			self.reloadCount -= 1
		#self.react += 1

	def updateRunning(self):
		self.rect.move_ip(0,3)
		if self.rect.top > Globals.Resolution[1] or self.rect.left > Globals.Resolution[0]:
			self.kill()

	def generateCreationImage(self):
		self.creationImage = pygame.Surface([self.shootingRange*2,self.shootingRange*2]).convert_alpha()
		self.creationImage.fill([0,0,0,0])
		self.rect = self.creationImage.get_rect()
		pygame.draw.circle(self.creationImage, [0,0,0,255], [self.shootingRange,self.shootingRange], self.shootingRange, 1)
		dest = self.rect.center[0]-self.tuxImage.get_rect().center[0], self.rect.center[1]-self.tuxImage.get_rect().center[1]
		self.creationImage.blit(self.tuxImage, dest)
		self.rect.center = pygame.mouse.get_pos()
		Globals.ObjectsExists.add(self)
		self.state = ATCREATION

	def rangeCollision(self):
		center = self.rect.center
		for Object in Globals.ObjectsTropical.sprites():
			dx = Object.rect.center[0] - center[0]
			dy = Object.rect.center[1] - center[1]
			d = math.sqrt(pow(dx,2)+pow(dy,2))
			if d < self.shootingRange and self.reloadCount == 0:
				S = LightSnowBall.LightSnowBall(center, Object.rect.center)
				Globals.ObjectsArcticBullets.add(S)
				Globals.ObjectsExists.add(S)
				self.reloadCount = 60

